import Vue from 'vue';
import store from '@/store';
import Rsa from '@/utils/rsa.js';
import { hideOfString } from '@/common/utils';
const plugin = {
  install(Vue, option) {
    // 添加全局方法或者属性
    Vue.myGlobalMethod = function() {};
    // 添加全局指令
    Vue.directive('focus', {
      inserted: function(el) {
        el.querySelector('input').focus();
      }
    });
    // 添加混入
    Vue.mixin({
      data() {
        return {
          curId: -1,
          fileUploadHeaders: {},
          pagePermission:
            this.$route && this.$route.meta && this.$route.meta.permission ? this.$route.meta.permission : '',
          organizeMix: [],
          //混入小区列表
          communitiesMix: [],
          CheckEncryptionValue: ''
        };
      },
      // 添加到mixin中的任何内容将被注入到所有组件中。
      // 在这个例子中， mounted() 方法将在组件被挂载到DOM后调用
      methods: {
        hideOfString(s) {
          return hideOfString(s);
        },
        async get_sensitive_fields(row, fn) {
          let auth = sessionStorage.getItem('CheckEncryption');
          let param = {
            auth,
            PID: row.PID
          };
          let res = await this.$api.user.get_sensitive_fields(param);
          if (res.code !== 0 || !res.data) {
            this.$message.error(res.msg);
            return;
          }
          // 解密
          let keyiv = JSON.parse(sessionStorage.getItem('keyiv'));
          let rsa = new Rsa(keyiv);
          let idCard = rsa.decrypt(res.data.idCard);
          let phone = rsa.decrypt(res.data.phone);
          // 回调
          let currentRow = Object.assign({ ...row }, { idCard, phone });
          fn(currentRow);
        },
        // 获取敏感信息时判断函数
        getCheckEncryption() {
          let rs = sessionStorage.getItem('CheckEncryption');
          if (rs) {
            this.CheckEncryptionValue = rs;
            return rs;
          } else {
            return false;
          }
        },
        getDictDataAll() {
          let dictList = JSON.parse(sessionStorage.getItem('dictDataAll'));
          return dictList ? dictList : [];
        },
        // 拼接图片
        getItemPhoto(id) {
          var url = this.global.baseUrlImage + 'avatar/' + id;
          return url;
        },
        setPageInfo: function(name, url) {
          var link = document.querySelector("link[rel*='icon']") || document.createElement('link');
          link.type = 'image/vnd.microsoft.icon';
          link.rel = 'shortcut icon';
          link.href = url;
          document.getElementsByTagName('head')[0].appendChild(link);
          var title = document.querySelector('title') || document.createElement('title');
          title.innerText = name;
          this.$store.commit('setSystemName', name);
          this.$store.commit('setSystemIconUrl', url);
        },
        cellEnter: function(row) {
          this.curId = row.id;
        },
        cellLeave: function(row) {
          this.curId = -1;
        },
        hasPerm: function(perms) {
          let permissions = store.state.user.perms.data || [];

          return Array.isArray(permissions) && new Set(permissions).has(perms);
        },
        getOrganizeMix() {
          let that = this;
          return new Promise(resolve => {
            if (!store.state.organize.organize.length) {
              that.$api.wxNotic.sys_party_organize().then(res => {
                if (res.code === 0) {
                  console.log('执行',res.data)
                  store.commit('updateOrga', res.data);
                }
                resolve(store.state.organize.organize);
              });
            } else {
              resolve(store.state.organize.organize);
            }
          });
        },

        getCommunitiesMix() {
          let that = this;
          return new Promise(resolve => {
            if (!store.state.organize.communities.length) {
              that.$api.three.communityList().then(res => {
                if (res.code === 0) {
                  store.commit('updateCom', res.data);
                }
                resolve(store.state.organize.communities);
              });
            } else {
              resolve(store.state.organize.communities);
            }
          });
        }
      },
      created() {
        if (sessionStorage) {
          this.fileUploadHeaders.Authorization = sessionStorage.getItem('token');
        }
      },
      beforeRouteLeave(to, from, next) {
        // this.scrollY = window.scrollY
        next();
      },
      beforeRounteEnter(to, from, next) {
        next(vm => {
          window.scroll(0, vm.scrollY);
        });
      }
    });
    // 添加实例方法
    // Vue.prototype.$xxx = function () {}
    // 注册全局组件
    Vue.component();
  }
};

Vue.use(plugin);
